<?php
namespace app\member\controller;
use app\common\controller\Base;
class Login extends Base{
    function login($name='',$pwd='',$code=''){
    	if(request()->isPost()){
	    	$validate=$this->validate([
	    		'name'=>$name,
	    		'pwd'=>$pwd,
	    		//'code'=>$code,
	    	],[
	    		'name|'.lang('User name')=>'require',
	    		'pwd|'.lang('User password')=>'require',
	    		//'code|'.lang('Verification code')=>'require|captcha',
	    	]);
	    	if(true!==$validate){
	    		$this->error($validate);
	    	}
	    	$rs=model('member')->read(['login_name'=>$name]);
	    	if($rs&&$rs['login_pwd']===encrypt_pwd($pwd)){//登录成功
	    		unset($rs['login_pwd']);
	    		// 如果没有统一创建一个微信消息采集的特殊任务
	    		$rs=$rs->toArray();
	    		session(config('keys.member_user'),$rs);
	    		if($rs['wxmsg_task_id']==0){
	    			$wxmsg_task_id=model('weixinlogs')->createWxMsgInfos($rs['id']);
		    		$rs['wxmsg_task_id']=$wxmsg_task_id;
		    		session(config('keys.member_user'),$rs);
	    		}
                $this->success(lang('Login was successful'),url('member/index'));
                die;
	    	}
	    	$this->error(lang('Incorrect username or password'));
    	}
    	return view();
    }
    function logout(){
        session(config('keys.member_user'),null);
        $this->success(lang('Cancellation of success'),url('login'));
    }
    function register(){
    	if(request()->isPost()){
	    	$data=$this->request->param();
	    	$validate=$this->validate([
	    		'name'=>$data['login_name'],
	    		'pwd'=>$data['login_pwd'],
	    	],[
	    		'name|'.lang('User name')=>'require',
	    		'pwd|'.lang('User password')=>'require',
	    	]);
	    	if(true!==$validate)return json($validate,1);

	    	$data['s_time']=time();
	    	$data['login_pwd']=encrypt_pwd($data['login_pwd']);

	    	$rs=model($this->table)->add($data);
	    	if(isset($rs['id'])){
	    		return json(lang('Register was successful'),0,$rs);
	    	}
	    	return json(lang('The system is busy'),1);
    	}
    	return view();
    }
}
